/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Shards.
 *
 * Tiberiumlabs Shards is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Shards is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class FileUtils {

    // <editor-fold defaultstate="collapsed" desc=" recursive file list ">

    /**
     * Scan folder for files and get full list of currently existing files.<br/>
     * Same as <code>getRecursiveFilesList(root, false)</code>.
     * @param root folder to begin with.
     * @return all files, that are under specified root.
     * @see #getRecursiveFilesList(File, boolean)
     */
    public static List<File> getRecursiveFilesList(File root) {
        return getRecursiveFilesList(root, false);
    }

    /**
     * Scan folder for files and get full list of currently existing files.<br/>
     * @param root folder to begin with.
     * @param includeFolders flag to include (or not) folders to the list.
     * @return all files (and folders if specified), that are under specified root.
     */
    public static List<File> getRecursiveFilesList(File root, boolean includeFolders) {
        return getRecursiveFilesList(root, includeFolders, null, null);
    }

    /**
     * Scan folder for files and get full list of currently existing files, that accepted by FilenameFilter.<br/>
     * <b>NOTE:</b> if filenameFilter will not accept folder - it will not be scanned recuresively.
     * @param root folder to begin with.
     * @param includeFolders flag to include (or not) folders to the list.
     * @param filenameFilter <code>FilenameFilter</code> to use for listing files.
     * @return all files (and folders if specified), that are under specified root and accepted by FilenameFilter.
     */
    public static List<File> getRecursiveFilesList(File root, boolean includeFolders, FilenameFilter filenameFilter) {
        return getRecursiveFilesList(root, includeFolders, null, filenameFilter);
    }

    /**
     * Scan folder for files and get full list of currently existing files, that accepted by FileFilter.<br/>
     * <b>NOTE:</b> if fileFilter will not accept folder - it will not be scanned recuresively.
     * @param root folder to begin with.
     * @param includeFolders flag to include (or not) folders to the list.
     * @param fileFilter <code>FileFilter</code> to use for listing files.
     * @return all files (and folders if specified), that are under specified root and accepted by FileFilter.
     */
    public static List<File> getRecursiveFilesList(File root, boolean includeFolders, FileFilter fileFilter) {
        return getRecursiveFilesList(root, includeFolders, fileFilter, null);
    }

    private static List<File> getRecursiveFilesList(File root, boolean includeFolders, FileFilter fileFilter, FilenameFilter filenameFilter) {
        ArrayList<File> result = new ArrayList<File>();
        addFilesRecursively(result, root, includeFolders, fileFilter, filenameFilter);
        return result;
    }

    private static void addFilesRecursively(List<File> filesList, File currentFile, boolean includeFolders, FileFilter fileFilter, FilenameFilter filenameFilter) {
        if (currentFile.isFile()) {
            filesList.add(currentFile);
        }
        else {
            if (includeFolders) {
                filesList.add(currentFile);
            }
            File[] files = null;
            if (fileFilter != null) {
                files = currentFile.listFiles(fileFilter);
            }
            else if (filenameFilter != null) {
                files = currentFile.listFiles(filenameFilter);
            }
            else {
                files = currentFile.listFiles();
            }
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    addFilesRecursively(filesList, files[i], includeFolders, fileFilter, filenameFilter);
                }
            }
        }
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc=" file name utils ">

    /**
     * Get next file in current folder. If current file does not exists - current
     * file is returned. If exists - "_N" added before extension, where N is next
     * number, that does not exists.<br/>
     * <b>NOTE:</b> in multithreaded execution make sure to create file in time.
     * As long as file is not created, this method may return same file name (next
     * not existing).
     * @param currentFile file to create next name for
     * @return same file if not exists, and next file name if exitst.
     */
    public static File getNextFile(File currentFile) {
        if (currentFile == null) {
            throw new NullPointerException();
        }
        // if current file not exists - return it
        if (!currentFile.exists()) {
            return currentFile;
        }
        // if we are here - currentFile already exists.
        File parent = currentFile.getParentFile();
        String fileFullName = currentFile.getName();
        int indexOfExt = fileFullName.indexOf('.');
        String name;
        String ext;
        if (indexOfExt > 0) {
            ext = fileFullName.substring(indexOfExt);
            name = fileFullName.substring(0, indexOfExt) + '_';
        }
        else {
            ext = "";
            name = fileFullName + '_';
        }
        int count = 0;
        File nextFile = currentFile;
        while (nextFile.exists()) {
            count++;
            nextFile = new File(parent, name + count + ext);
        }
        return nextFile;
    }

    // </editor-fold>

}
